package com.yixia.pea.controller;

import com.yixia.common.core.controller.BaseController;
import com.yixia.common.core.domain.AjaxResult;
import com.yixia.common.utils.page.TableDataInfo;
import com.yixia.pea.domain.credit.LoansBank;
import com.yixia.pea.service.LoansBankService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 依梦
 * @Date 2023/9/7
 */
@ApiOperation("银行管理模块")
@Slf4j
@RestController
@RequestMapping("/credit/loansBank")
public class LoansBankController extends BaseController {

    @Autowired
    private LoansBankService loansBankService;

    // 银行管理
    @GetMapping("/list")
    @PreAuthorize("@yixia.hasPermi('cre:bank:list')")
    public TableDataInfo list(LoansBank loansBank){
        // 进行查询
        startPageByHelper();
        List<LoansBank> list = loansBankService.selectPageList(loansBank);
        return getDataTable(list);
    }

    // 添加银行信息
    @PostMapping("/add")
    @PreAuthorize("@yixia.hasPermi('cre:bank:add')")
    public AjaxResult add(@RequestBody LoansBank loansBank){
        // 检查该农行是否已经存在---检查的是编号
        if(!loansBankService.checkBankNumUnique(loansBank)){
            return error("银行编号已存在，请重新添加！");
        }
        if(!loansBankService.checkBankNameUnique(loansBank)){
            return error("银行名已存在，请重新添加！");
        }
        return toAjax(loansBankService.save(loansBank));
    }

    // 修改银行信息
    @PutMapping("/edit")
    @PreAuthorize(("@yixia.hasPermi('cre:bank:edit')"))
    public AjaxResult edit(@RequestBody LoansBank loansBank){
        // 检查该农行是否已经存在---检查的是编号
        if(!loansBankService.checkBankNumUnique(loansBank)){
            return error("银行编号已存在，请重新添加！");
        }
        if(!loansBankService.checkBankNameUnique(loansBank)){
            return error("银行名已存在，请重新添加！");
        }
        return toAjax(loansBankService.updateById(loansBank));
    }

    // 删除银行信息
    @DeleteMapping("/remove/{bankIds}")
    @PreAuthorize(("@yixia.hasPermi('cre:bank:remove')"))
    public AjaxResult remove(@PathVariable Long[] bankIds){
        return toAjax(loansBankService.removeByBankIds(bankIds));
    }

    // 获取详细的银行信息
    @GetMapping("/info/{bankId}")
    @PreAuthorize("@yixia.hasPermi('cre:bank:list')")
    public AjaxResult info(@PathVariable Long bankId){
        // 判断该银行数据是否存在
        if(!loansBankService.checkBankDataUnique(bankId)){
            return error("操作错误，数据不存在！");
        }
        return AjaxResult.success(loansBankService.getByBankId(bankId));
    }

    // 修改银行状态
    @PutMapping("/changeStatus")
    @PreAuthorize("@yixia.hasPermi('cre:bank:edit')")
    public AjaxResult changeStatus(@RequestBody LoansBank loansBank){
        // 判断该银行数据是否存在
        if(!loansBankService.checkBankDataUnique(loansBank.getBankId())){
            return error("操作错误，数据不存在！");
        }
        return AjaxResult.success(loansBankService.updateById(loansBank));
    }
}
